<?php
	namespace ampleharvest\task\metric;
	use\ampleharvest\code;

	require_once dirname(dirname(dirname(dirname(__FILE__)))) . "/config/cConfig.php";
	$codeConfig = new \ampleharvest\code\cConfig();
	
	require_once dirname(dirname(dirname(dirname(__FILE__)))) . "/abstract_classes/abs_Task.php";
	require_once $codeConfig->C_STR_USER_MANAGER_PATH . "/Classes/cTABLE_Users.php";
	require_once dirname(dirname(dirname(dirname(__FILE__)))) . "/classes/Tables/cTABLE_Metric.php";

	class count_ActiveUsers extends \abs_Task
	{
		const C_STR_NAME = "count_ActiveUsers";

		const C_INT_ARRAY_START_DATE = 0;
		const C_INT_ARRAY_DATA = 1;

		private $m_obj_TABLE_Metric = null;
		private $m_obj_TABLE_Users = null;

		public function __construct($str_LockPath)
		{
			$this->set_LockPath($str_LockPath);
			$this->set_Name(self::C_STR_NAME);
			$this->m_obj_TABLE_Users = new \cTABLE_Users();
			$this->m_obj_TABLE_Metric = new \cTABLE_Metric();
			parent::__construct();
		}

		public function run($bool_Force = false)
		{
			$this->set_Force($bool_Force);
			if (!parent::run())
			{
				return false;
			}
			$arr_Data = array();

			$obj_CurrentDate = new \DateTime();
			$obj_CurrentDate->setTime(0, 0, 0);

			$obj_StartYear = clone $obj_CurrentDate;
			$obj_StartYear->setDate($obj_CurrentDate->format("Y"), 1, 1);

			$obj_StartMonth = clone $obj_CurrentDate;
			$obj_StartMonth->setDate($obj_CurrentDate->format("Y"), $obj_CurrentDate->format("m"), 1);

			$obj_StartHour = new \DateTime();
			$obj_StartHour->setTime($obj_StartHour->format("H"), 0, 0);

			$obj_StartDay = clone $obj_CurrentDate;
/*
			print("Current date - " . $obj_CurrentDate->format("c") . "\n");
			print("StartYear - " . $obj_StartYear->format("c") . "\n");
			print("StartMonth - " . $obj_StartMonth->format("c") . "\n");
			print("StartDay - " . $obj_StartDay->format("c") . "\n");
			print("StartHour - " . $obj_StartHour->format("c") . "\n");
*/
			//$this->m_obj_TABLE_Metric->clear_Query();
			$arr_Data[\cTABLE_Metric::C_STR_SUBCATEGORY_HOURLY][self::C_INT_ARRAY_START_DATE] = $obj_StartHour;
			$arr_Data[\cTABLE_Metric::C_STR_SUBCATEGORY_HOURLY][self::C_INT_ARRAY_DATA] = $this->collect_Count($obj_StartHour);
			$arr_Data[\cTABLE_Metric::C_STR_SUBCATEGORY_DAILY][self::C_INT_ARRAY_START_DATE] = $obj_StartDay;
			$arr_Data[\cTABLE_Metric::C_STR_SUBCATEGORY_DAILY][self::C_INT_ARRAY_DATA] = $this->collect_Count($obj_StartDay);
			$arr_Data[\cTABLE_Metric::C_STR_SUBCATEGORY_MONTHLY][self::C_INT_ARRAY_START_DATE] = $obj_StartMonth;
			$arr_Data[\cTABLE_Metric::C_STR_SUBCATEGORY_MONTHLY][self::C_INT_ARRAY_DATA] = $this->collect_Count($obj_StartMonth);
			$arr_Data[\cTABLE_Metric::C_STR_SUBCATEGORY_YEARLY][self::C_INT_ARRAY_START_DATE] = $obj_StartYear;
			$arr_Data[\cTABLE_Metric::C_STR_SUBCATEGORY_YEARLY][self::C_INT_ARRAY_DATA] = $this->collect_Count($obj_StartYear);
/*
			foreach ($arr_Data as $key => $arr_Source)
			{
				print($key . " - " . $arr_Source . "\n");
			}
*/
			foreach ($arr_Data as $str_SubCat => $arr_Count)
			{
				$this->m_obj_TABLE_Metric->clear_Query();

				$this->m_obj_TABLE_Metric->add_ColumnQuery($this->m_obj_TABLE_Metric->m_int_COL_Category, \cTABLE_Metric::C_STR_CATEGORY_TOTAL_USERS);
				$this->m_obj_TABLE_Metric->add_ColumnQuery($this->m_obj_TABLE_Metric->m_int_COL_Group, \cTABLE_Metric::C_STR_GROUP_ACTIVE);
				$this->m_obj_TABLE_Metric->add_ColumnQuery($this->m_obj_TABLE_Metric->m_int_COL_Subcategory, $str_SubCat);
				$this->m_obj_TABLE_Metric->add_ColumnQuery($this->m_obj_TABLE_Metric->m_int_COL_DateTime, $arr_Count[self::C_INT_ARRAY_START_DATE]->format("Y-m-d H:i:s"),
					\cCondition::c_str_OPERATOR_GREATER);

				$this->m_obj_TABLE_Metric->clear();

				if ($this->m_obj_TABLE_Metric->do_Search() > 0)
				{
					$arr_Results = $this->m_obj_TABLE_Metric->get_ResultSet();
					if (isset($arr_Results[0]))
					{
						$this->m_obj_TABLE_Metric->load_DataFromArray($arr_Results[0]);
						$obj_Date = new \DateTime();
						$this->m_obj_TABLE_Metric->set_datetime($obj_Date->format("Y-m-d H:i:s"));
					}
				}

				$this->m_obj_TABLE_Metric->set_category(\cTABLE_Metric::C_STR_CATEGORY_TOTAL_USERS);
				$this->m_obj_TABLE_Metric->set_group(\cTABLE_Metric::C_STR_GROUP_ACTIVE);
				$this->m_obj_TABLE_Metric->set_subcategory($str_SubCat);
				$this->m_obj_TABLE_Metric->set_value($arr_Count[self::C_INT_ARRAY_DATA]);

				$this->m_obj_TABLE_Metric->save_Data2DB();

			}

			return true;
		}

		private function collect_Count(\DateTime $obj_StartDate = null, \DateTime $obj_EndDate = null)
		{
			$this->m_obj_TABLE_Users->clear_Query();

			$this->m_obj_TABLE_Users->add_ReturnFunction("count(*)", "count");
			if ($obj_StartDate)
			{
				$this->m_obj_TABLE_Users->add_ColumnQuery($this->m_obj_TABLE_Users->m_int_COL_LastLogin, $obj_StartDate->format("Y-m-d H:i:s"),
					\cCondition::c_str_OPERATOR_GREATER);
			}

			$this->m_obj_TABLE_Users->do_Search();
			$arr_TempRS = $this->m_obj_TABLE_Users->get_ResultSet();

			return $arr_TempRS[0]["count"];
		}
	}
?>